package com.itheima.CSM_bacK.aabServices;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.CSM_bacK.aabServices.Serv.CheckGroupServ;
import com.itheima.CSM_bacK.aacMapper.CheckGroupMapper;
import com.itheima.CSM_bacK.aacMapper.CheckItemMapper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.pojo.DTO.DTOcGroupA;
import com.itheima.pojo.DTO.DTOcGroupE;
import com.itheima.pojo.Entity.CheckGroup;
import com.itheima.pojo.VO.VOcGroupFA;
import com.itheima.pojo.VO.VOcGroupFBI;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;


@Service
public class CheckGroupImpl implements CheckGroupServ {
    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(List<Integer> checkitemIds, @RequestBody DTOcGroupA kk) {
        CheckGroup checkGroup = new CheckGroup();
        BeanUtils.copyProperties(kk, checkGroup);
        checkGroupMapper.add(checkGroup);
        int groupId = checkGroup.getId();
        for (int i = 0; i < checkitemIds.size(); i++) {
            checkGroupMapper.addAtConnection(checkitemIds.get(i), groupId);
        }


    }

    @Override
    public void deleteById(Integer id) {
        checkGroupMapper.deleteById(id);
        int groupId = id;
        checkGroupMapper.deleteAtConnection(groupId);
    }

    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer setmealId) {
        List<Integer> kks = checkGroupMapper.findCheckGroupIdsBySetmealId(setmealId);
        return kks;
    }

    @Override
    public PageResult findPage(QueryPageBean kk) {
        PageHelper.startPage(kk.getCurrentPage(), kk.getPageSize());
        Page<CheckGroup> kks = checkGroupMapper.findPage(kk.getQueryString());
        return new PageResult(kks.getTotal(), kks.getResult());
    }

    @Override
    public void edit(List<Integer> checkitemIds, DTOcGroupE kk) {
        CheckGroup checkGroup = new CheckGroup();
        BeanUtils.copyProperties(kk, checkGroup);
        checkGroupMapper.edit(checkGroup);
        int groupId = checkGroup.getId();
        checkGroupMapper.deleteAtConnection(groupId);
        for (int i = 0; i < checkitemIds.size(); i++) {
            checkGroupMapper.addAtConnection(checkitemIds.get(i), groupId);
        }
    }

    @Override
    public List<VOcGroupFA> findAll() {
        List<CheckGroup> kks = checkGroupMapper.findAll();
        List<VOcGroupFA> vvo = new Page<>();
        //kks->vvo
        for (CheckGroup checkGroup : kks) {
            VOcGroupFA vo = new VOcGroupFA();
            BeanUtils.copyProperties(checkGroup, vo);
            vvo.add(vo);
        }
        return vvo;
    }

    @Override
    public VOcGroupFBI findById(Integer id) {
        CheckGroup checkGroup = checkGroupMapper.findById(id);
        VOcGroupFBI vo = new VOcGroupFBI();
        if (checkGroup != null) {
            BeanUtils.copyProperties(checkGroup, vo);
        }

        return vo;
    }
}


